<template>
  <div class="mod-config">
    <el-form :inline="true" :model="searchForm" @keyup.enter.native="queryList()">

      <el-form-item>
        <el-input v-model="searchForm.azCode" placeholder="地域编号" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-input v-model="searchForm.agentIpv4" placeholder="代理实例IP" clearable></el-input>
      </el-form-item>
      <el-form-item>
        <el-select v-model="searchForm.onlineStatus" filterable placeholder="请选择在线状态" style="width:100%">
          <el-option label="全部" value="" ></el-option>
          <template v-for="(item, index) in dict.OnlineStatus">
            <el-option :label="item.value +': '+ item.label" :value="item.value"></el-option>
          </template>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="queryList(1)">查询</el-button>
<!--        <el-button type="" @click="addEntity()">新增</el-button>
        <el-button type="danger" @click="deleteEntity()" :disabled="dataListSelections.length <= 0">批量删除</el-button>-->
      </el-form-item>
    </el-form>

    <el-table
      :data="dataList"
      border
      v-loading="dataListLoading"

      style="width: 100%;">
      <!--      @selection-change="selectionChangeHandle"-->
<!--           <el-table-column type="selection" header-align="center" align="center" width="50"></el-table-column>-->

<!--           <el-table-column prop="id"  label="主键" header-align="center" align="center"></el-table-column>-->
           <!--<el-table-column prop="azCode"  label="地域编号" header-align="center" align="center"></el-table-column>-->
           <el-table-column prop="agentHostname"  label="代理实例主机名" header-align="center" align="center"></el-table-column>
           <el-table-column prop="agentIpv4"  label="代理实例IP" header-align="center" align="center"></el-table-column>
           <el-table-column prop="agentPort"  label="代理实例服务端口" header-align="center" align="center"></el-table-column>
           <el-table-column prop="onlineStatus"  label="在线状态" header-align="center" align="center" >
             <template slot-scope="scope">
               <template v-for="(item, index) in dict.OnlineStatus">
                 <el-tag v-if="item.value == scope.row.onlineStatus" :type="item.style">
                   {{item.value}}: {{item.label}}
                 </el-tag>
               </template>
             </template>
           </el-table-column>
      <el-table-column prop="forceOffline"  label="禁止上线" header-align="center" align="center">
        <template slot-scope="scope">
          <template v-for="(item, index) in dict.Offline">
            <el-tag v-if="item.value == scope.row.forceOffline" :type="item.style">
              {{item.value}}: {{item.label}}
            </el-tag>
          </template>
        </template>
      </el-table-column>
<!--           <el-table-column prop="agentDesc"  label="执行器描述" header-align="center" align="center"></el-table-column>-->
           <el-table-column prop="bizCode"  label="业务系统编号" header-align="center" align="center"></el-table-column>
<!--           <el-table-column prop="createTime"  label="创建时间" header-align="center" align="center"></el-table-column>-->
<!--           <el-table-column prop="version"  label="版本号" header-align="center" align="center"></el-table-column>-->
<!--           <el-table-column prop="updateTime"  label="更新时间戳" header-align="center" align="center"></el-table-column>-->

           <el-table-column label="操作" fixed="right" header-align="center" align="center" width="150">
             <template slot-scope="scope">
               <el-button type="text" size="small" @click="showDetail(scope.row)">明细</el-button>
               <el-button type="text" size="small" @click="offline(scope.row, '0')">下线</el-button>
               <el-button v-if="scope.row.forceOffline == 0" type="text" size="small" @click="offline(scope.row, '1')">强制下线</el-button>
               <el-button v-if="scope.row.forceOffline == 1" type="text" size="small" @click="online(scope.row)">可上线</el-button>
             </template>
           </el-table-column>
    </el-table>
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="page"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
    <!-- 弹窗, 新增 / 修改 -->
<!--    <add-entity v-if="addEntityVisible" ref="addEntity" @refreshDataList="queryList"></add-entity>-->
<!--    <mod-entity v-if="modEntityVisible" ref="modEntity" @refreshDataList="queryList"></mod-entity>-->
    <show-detail v-if="showDetailVisible" ref="showDetail"></show-detail>
  </div>
</template>

<script>
  import ShowDetail from './agent-registry-detail'
  import {mapState} from "vuex";

  export default {
    data() {
      return {
        isShow: false,
        searchForm: {
        },
        dataList: [],
        page: 1,
        pageSize: 10,
        totalPage: 0,
        dataListLoading: false,
        dataListSelections: [],
        addEntityVisible: false,
        modEntityVisible: false,
        showDetailVisible: false,
      }
    },
    components: {
      ShowDetail
    },
    activated() {
      this.queryList()
    },
    computed: mapState({
       // listType: state => state.goods.listType
    }),
    mounted() {
      // 数据字典查询
      //this.getSelectDict('FlowType').then((res) => {
      //  this.$store.commit('goods/updateListType', res)
      //})
    },
    methods: {
      // 获取数据列表
      queryList(clr) {
        if (typeof (clr) != 'undefined') {
          this.page = 1
        }
        this.dataListLoading = true
        let obj = {
          'page': this.page,
          'size': this.pageSize,
          //isTrans: true,
          //transParams: ['FlowType', 'ApplyResult', 'ReceiveStatus'],
          //transDict: ['flowType', 'applyResult', 'receiveStatus']
        }
        obj = Object.assign(obj, this.searchForm)
        this.getTableList('/api/agent/list', obj, (data) => {
          this.dataListLoading = false
          if (data.code == '000000' && data.rows && data.rows.length > 0) {
            this.dataList = data.rows
            this.dataList.forEach(it => {
              if(it.onlineStatus == 'F'){
                it.onlineStatusDesc= '离线';
              }else if(it.onlineStatus == 'H'){
                it.onlineStatusDesc= '在服';
              }
            });
            this.totalPage = data.total
          } else {
            this.dataList = []
            this.totalPage = 0
          }
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.page = 1
        this.queryList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.page = val
        this.queryList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      filterTag(value, row) {
        return row.onlineStatus === value;
      },
      // 新增
      showDetail(row) {
        let cloneData = Object.assign({}, row)
        this.showDetailVisible = true
        this.$nextTick(() => {
          this.$refs.showDetail.init(cloneData.id, cloneData)
        })
      },
      offline(row, force){
        let obj = {
          'id': row.id,
          'forceOffline': force,
        }
        // obj = Object.assign(obj, this.searchForm)
        this.getTableList('/api/agent/offline', obj, (data) => {
          if (data && data.code === '000000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000,
              onClose: () => {
              }
            });
            this.queryList();
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      online(row){
        let obj = {
          'id': row.id,
        }
        this.getTableList('/api/agent/online', obj, (data) => {
          if (data && data.code === '000000') {
            this.$message({
              message: '操作成功',
              type: 'success',
              duration: 1000,
              onClose: () => {
              }
            });
            this.queryList();
          } else {
            this.$message.error(data.msg)
          }
        })
      },
      // 删除
      // deleteEntity(id) {
      //   var ids = id ? [id] : this.dataListSelections.map(item => {
      //     return item.id
      //   })
      //   this.$confirm(`确定对[id=${ids.join(',')}]进行删除操作?`, '提示', {
      //     confirmButtonText: '确定',
      //     cancelButtonText: '取消',
      //     type: 'warning'
      //   }).then(() => {
      //     	let obj = {"ids" : ids}
      //       this.getTableList('/api/agentRegistryListDel', obj, (res) => {
      //       if (res && res.code ===  '000000' ) {
      //         this.$message({
      //           message: '操作成功',
      //           type: 'success',
      //           duration: 1000,
      //           onClose: () => {
      //             this.queryList()
      //           }
      //         })
      //       } else {
      //         this.$message.error(res.rsmsg)
      //       }
      //     })
      //   })
      // }
    }
  }
</script>